﻿using System;
using System.Threading.Tasks;
using Hn.Command;
using Hn.Database.DAL;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MSocketCore.Hub;
using SuperSocket;
using SuperSocket.Command;

namespace MSocketCore.MSocket
{
    [MyCmdFilter]
    [Command(Key = "0A")]
    public class AFN0A : IAsyncCommand<MySession,MyPackage>
    {
        private readonly MessageClient _client;
        private readonly ILogger<AFN0A> _logger;

        public AFN0A(MessageClient client,ILogger<AFN0A> logger)
        {
            _client = client;
            _logger = logger;
        }

        public void Sub1(MyRequestInfo info)
        {
            int heart = info.Content[5];
            var dtu = DtuDAL.GetBySn(info.Sn);
            DtuExtendDAL.UpdatHeart(dtu.Id, dtu.Sn, heart);
            var name = UserDAL.GetUserIdBySn(info.Sn);
            _client.SendInfo(name, info.Sn + " Heart : " + heart);
        }

        public ValueTask ExecuteAsync(MySession session, MyPackage info)
        {
            _logger.LogInformation($"App Recevice:{info.Sn}[Query]\n{info.Message}");
            switch (info._Fn)
            {
                case 1:
                    Sub1(info);
                    break;
            }

            return  new ValueTask();
        }
    }
}
